class: center, middle, inverse, title-slide .title[ # Data Visualization ] .subtitle[ ## Usage of unhcthemes package ] --- <style type="text/css"> .primary { color: var(--unhcr-blue); } .w-75 { max-width: 75%; } .w-50 { max-width: 50%; } .w-25 { max-width: 25%; } </style> # Objectives **About today:** - Basic of the grammar of graphics and [**`ggplot2`**](https://ggplot2.tidyverse.org/index.html) - Introduction to the [**`unhcrthemes`**](https://vidonne.github.io/unhcrthemes/) package - Best practices to create **UNHCR branded** visuals in R, with examples - What else, dynamic report with the [**`unhcrdown`**](https://github.com/vidonne/unhcrdown) **Not today:** - Data import: [`readr`](https://readr.tidyverse.org/), [`readxl`](https://readxl.tidyverse.org/), etc. - Data manipulation: [`dplyr`](https://dplyr.tidyverse.org/), [`tidyr`](https://tidyr.tidyverse.org/), etc. - R programming: [R for Data Science](https://r4ds.had.co.nz/), [Advanced R Programming](https://adv-r.hadley.nz/), etc. --- class: inverse, center, middle # `ggplot2` and `unhcrthemes` ### Introduction --- # The `ggplot2` package .pull-left[ - **`ggplot2`** is an R package for declaratively creating graphics - **`ggplot2`** is an implementation [The Grammar of Graphics](https://link.springer.com/chapter/10.1007/978-3-642-21551-3_13) by Leland Irving - **The idea** don't start with the final form of the graphic (Excel approach) but **decompose the graphic** into its constituents ] .pull-right[ .center[] ] ??? You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details. What does it take to create a graphic? Data, axis, geometric objects, etc. --- # Structure of `ggplot2` How a **`ggplot2`** graph is built on the grammar of graphics elements: -- 1. **Data .primary[`ggplot(data)`]:** The raw data that you want to plot. -- 2. **Aesthetics .primary[`aes()`]:** Aesthetics mappings of the geometric and statistical objects, such as position, color, size, shape, and transparency -- 3. **Geometries .primary[`geom_*()`]:** The geometric shapes that will represent the data. -- 4. **Statistical transformations .primary[`stat_*()`]:** Statistical summaries of the data, such as quantiles, fitted curves, and sums. -- 5. **Scales .primary[`scale_*()`]:** Maps between the data and the aesthetic dimensions, such as data range to plot width or factor values to colors. -- 6. **Coordinate system .primary[`coord_*()`]:** The transformation used for mapping data coordinates into the plane of the data rectangle. -- 7. **Facets .primary[`facet_*()`]:** The arrangement of the data into a grid of plots. -- 8. **Visual themes .primary[`theme_*()`]:** The overall visual defaults of a plot, such as background, grids, axes, default typeface, sizes and colors. ??? 1. Data - without data, you don't have a plot! 2. Mapping - linking variables to graphical properties. 3. Geometries - interpret aesthetics as graphical representations. 4. Statistics - compute/transform numbers for us. 5. Scales - interpret values in data to graphical properties. 6. Coordinates - define physical mapping. 7. Facets - split plot into panels. 8. Theme - what does your plot look like? --- # The `unhcrthemes` package .pull-left[ 1. Adjusted `ggplot2` theme 2. A series of color palette for: - A **categorical palette** for UNHCR main data visualization colors - A **categorical palette** for people of concern to UNHCR categories - A **categorical palette** for geographical regional divisions of UNHCR - Six **sequential color palettes** for all the main data visualization colors - Two recommended **diverging color palette** ] .pull-right[ .center[<img src="https://raw.githubusercontent.com/vidonne/unhcrthemes/master/man/figures/unhcrthemes_sticker.png" alt="unhcrthemes HEX" style="max-width:60%">] ] ??? You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details. What does it take to create a graphic? Data, axis, geometric objects, etc. --- class: inverse, center, middle # `ggplot2` and `unhcrthemes` ### In action --- ## Setup A step-by-step on how to build a UNHCR branded chart with `ggplot2` and `unhcrthemes` packages, using an example chart from [Global Trends 2021](https://www.unhcr.org/globaltrends.html). .pull-left[ **Load the required packages and data** ```r # Load packages library(tidyverse) library(unhcrthemes) # Load data displ <- read_csv("data/displaced_pop.csv") ``` ] .pull-right[ <table> <thead> <tr> <th style="text-align:right;"> year </th> <th style="text-align:left;"> pop </th> <th style="text-align:right;"> num </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 2012 </td> <td style="text-align:left;"> IDPs </td> <td style="text-align:right;"> 26387120 </td> </tr> <tr> <td style="text-align:right;"> 2013 </td> <td style="text-align:left;"> IDPs </td> <td style="text-align:right;"> 33340830 </td> </tr> <tr> <td style="text-align:right;"> 2014 </td> <td style="text-align:left;"> IDPs </td> <td style="text-align:right;"> 37877320 </td> </tr> <tr> <td style="text-align:right;"> 2015 </td> <td style="text-align:left;"> IDPs </td> <td style="text-align:right;"> 40451900 </td> </tr> <tr> <td style="text-align:right;"> 2016 </td> <td style="text-align:left;"> IDPs </td> <td style="text-align:right;"> 40220850 </td> </tr> <tr> <td style="text-align:right;"> 2017 </td> <td style="text-align:left;"> IDPs </td> <td style="text-align:right;"> 39934042 </td> </tr> </tbody> </table> ] --- ## Data .pull-left[ ```r *ggplot(data = displ) ``` ] .pull-right[ <img src="data:image/png;base64,#usage_unhcrthemes_stats_files/figure-html/unnamed-chunk-6-1.png" width="2100" /> ] ??? 1. Data - without data, you don't have a plot! But nothing happens here because we haven't mapped the raw data to anything. SO we just get a empty canvas. --- ## Aesthetics .pull-left[ ```r ggplot(data = displ, * aes(x = year, y = num)) ``` ] .pull-right[ <img src="data:image/png;base64,#usage_unhcrthemes_stats_files/figure-html/unnamed-chunk-8-1.png" width="2100" /> ] ??? 2. Mapping - linking variables to graphical properties. We have now mapped the year to the x axis and the number displaced to y but we still don't see anything special except the axis value --- ## Geoms .pull-left[ ```r ggplot(data = displ, aes(x = year, y = num)) + * geom_col() ``` ] .pull-right[ <img src="data:image/png;base64,#usage_unhcrthemes_stats_files/figure-html/unnamed-chunk-10-1.png" width="2100" /> ] --- ## Scale .pull-left[ ```r ggplot(data = displ, aes(x = year, y = num)) + geom_col() + * scale_x_continuous( * breaks = scales::pretty_breaks(n = 10)) ``` ] .pull-right[ <img src="data:image/png;base64,#usage_unhcrthemes_stats_files/figure-html/unnamed-chunk-12-1.png" width="2100" /> ] --- ## Scale .pull-left[ ```r ggplot(data = displ, aes(x = year, y = num)) + geom_col() + scale_x_continuous( breaks = scales::pretty_breaks(n = 10)) + * scale_y_continuous( * labels = scales::label_number_si(), * expand = expansion(c(0, 0.1))) ``` ] .pull-right[ <img src="data:image/png;base64,#usage_unhcrthemes_stats_files/figure-html/unnamed-chunk-14-1.png" width="2100" /> ] --- ## Theme .pull-left[ Before playing with `unhcthemes` let's add some information on the chart. ```r ggplot(data = displ, aes(x = year, y = num)) + geom_col() + scale_x_continuous( breaks = scales::pretty_breaks(n = 10)) + scale_y_continuous( labels = scales::label_number_si(), expand = expansion(c(0, 0.1))) + * labs(title = "People forced to flee worldwide | 2012-2022", * caption = "Source: UNHCR Refugee Data Finder") ``` ] .pull-right[ <img src="data:image/png;base64,#usage_unhcrthemes_stats_files/figure-html/unnamed-chunk-16-1.png" width="2100" /> ] --- ## Theme .pull-left[ ```r ggplot(data = displ, aes(x = year, y = num)) + geom_col() + scale_x_continuous( breaks = scales::pretty_breaks(n = 10)) + scale_y_continuous( labels = scales::label_number_si(), expand = expansion(c(0, 0.1))) + labs(title = "People forced to flee worldwide | 2012-2022", caption = "Source: UNHCR Refugee Data Finder") + * theme_unhcr() ``` ] .pull-right[ <img src="data:image/png;base64,#usage_unhcrthemes_stats_files/figure-html/unnamed-chunk-18-1.png" width="2100" /> ] --- ## Theme .pull-left[ ```r ggplot(data = displ, aes(x = year, y = num)) + geom_col() + scale_x_continuous( breaks = scales::pretty_breaks(n = 10)) + scale_y_continuous( labels = scales::label_number_si(), expand = expansion(c(0, 0.1))) + labs(title = "People forced to flee worldwide | 2012-2022", caption = "Source: UNHCR Refugee Data Finder") + * theme_unhcr(grid = "Y", * axis_title = FALSE) ``` ] .pull-right[ <img src="data:image/png;base64,#usage_unhcrthemes_stats_files/figure-html/unnamed-chunk-20-1.png" width="2100" /> ] --- ## Theme .pull-left[ ```r ggplot(data = displ, aes(x = year, y = num)) + geom_col( * color = unhcr_pal(n = 1, name = "pal_blue") ) + scale_x_continuous( breaks = scales::pretty_breaks(n = 10)) + scale_y_continuous( labels = scales::label_number_si(), expand = expansion(c(0, 0.1))) + labs(title = "People forced to flee worldwide | 2012-2022", caption = "Source: UNHCR Refugee Data Finder") + theme_unhcr(grid = "Y", axis_title = FALSE) ``` ] -- .pull-right[ <img src="data:image/png;base64,#usage_unhcrthemes_stats_files/figure-html/unnamed-chunk-22-1.png" width="2100" /> ] ??? Is color the right property? Also notice that we haven't mapped the color to anything but we're just setting it. --- ## Theme .pull-left[ ```r ggplot(data = displ, aes(x = year, y = num)) + geom_col( * fill = unhcr_pal(n = 1, name = "pal_blue") ) + scale_x_continuous( breaks = scales::pretty_breaks(n = 10)) + scale_y_continuous( labels = scales::label_number_si(), expand = expansion(c(0, 0.1))) + labs(title = "People forced to flee worldwide | 2012-2022", caption = "Source: UNHCR Refugee Data Finder") + theme_unhcr(grid = "Y", axis_title = FALSE) ``` ] .pull-right[ <img src="data:image/png;base64,#usage_unhcrthemes_stats_files/figure-html/unnamed-chunk-24-1.png" width="2100" /> ] ??? Is color the right property? Also notice that we haven't mapped the color to anything but we're just setting it. --- ## Theme .pull-left[ ```r ggplot(data = displ, aes(x = year, y = num, * fill = pop)) + geom_col() + scale_x_continuous( breaks = scales::pretty_breaks(n = 10)) + scale_y_continuous( labels = scales::label_number_si(), expand = expansion(c(0, 0.1))) + labs(title = "People forced to flee worldwide | 2012-2022", caption = "Source: UNHCR Refugee Data Finder") + theme_unhcr(grid = "Y", axis_title = FALSE) ``` ] .pull-right[ <img src="data:image/png;base64,#usage_unhcrthemes_stats_files/figure-html/unnamed-chunk-26-1.png" width="2100" /> ] --- class: inverse, center, middle # Thank you ### Questions?